<template>
    <view class="footer-view" :style="{ height: listHeight + 'rpx' }">
        <eui-card title="基本信息" showTitleLine="true" h-padding="20" type="upDownSingle">
            <template v-slot:content>
                <view class="m-main-top">
                    <eui-text v-model="mainData.c_adno" type="titlename" title="部门"
                        :name="getRelatedName('rdsDeparts', mainData.c_adno)" />
                    <eui-text v-model="mainData.c_effect_da" type="date" title="生效日期" />
                    <eui-text v-model="mainData.c_apply_userno"
                        :name="getRelatedName('rdsUsers', mainData.c_apply_userno)" type="titlename" title="申请人" />
                    <eui-text v-model="mainData.c_buy_userno" :name="getRelatedName('rdsUsers', mainData.c_buy_userno)"
                        type="titlename" title="采购员" />
                    <eui-text v-model="mainData.c_exec_dt" type="rdstime" title="执行时间" occupy />
                    <eui-text v-model="mainData.c_mk_userno" :name="getRelatedName('rdsUsers', mainData.c_mk_userno)"
                        type="titlename" title="录入" />
                    <eui-text v-model="mainData.c_mk_dt" type="rdstime" />
                </view>
            </template>
        </eui-card>
        <view class="paging-view">
            <eui-paging ref="paging" v-model="listManager" :fixed="false" :refresher-enabled="false"
                :default-page-size="5" :loading-more-enabled="true" :show-loading-more-when-reload="true">
                <view v-for="(item, index) in listManager" class="m-list-top">
                    <eui-text :value="item.c_name" type="contrast" :title="index + 1 + '. 商品'"
                        :oldValue="item.c_name_old" occupy />
                    <eui-text :value="item.c_adno" type="titlename" title="部门"
                        :name="getRelatedName('rdsDeparts', item.c_adno)" />
                    <eui-text :value="getRelatedName('rdsGdsclasses', item.c_ccode)" type="name" title="品类" />
                    <eui-text :value="item.c_bcode" :oldValue="item.c_bcode_old_name" type="contrast" title="品牌" />
                    <eui-text :value="item.c_pycode" :oldValue="item.c_pycode_old" type="contrast" title="拼音码" />
                    <eui-text :value="item.c_name_en" :oldValue="item.c_name_en_old" type="contrast" title="外文名称" />
                    <eui-text :value="getDictName('品类类型', item.c_gdsclass_type)" :oldValue="item.c_gdsclass_type_old_name" title="品类类型" type="contrast" />
                    <eui-text :value="item.c_pluno" type="contrast" :oldValue="item.c_pluno_old" title="PLU码" />
                    <eui-text :value="getDictName('称重/计件类型', item.c_plu_type)" type="contrast" :oldValue="item.c_plu_type_old" title="计件类型" />
                    <eui-text :value="item.c_model" :oldValue="item.c_model_old" type="contrast" title="规格" />
                    <eui-text :value="item.c_basic_unit" :oldValue="item.c_basic_unit_old" type="contrast" title="单位" />
                    <eui-text :value="item.c_order_content" :oldValue="item.c_order_content_old" type="contrastnumber" title="订货含量" />
                    <eui-text :value="item.c_order_unit" :oldValue="item.c_order_unit_old" type="contrast" title="订货单位" />
                    <eui-text :value="item.c_distr_content" :oldValue="item.c_distr_content_old" type="contrastnumber" title="配货含量" />
                    <eui-text :value="item.c_distr_unit" :oldValue="item.c_distr_unit_old" type="contrast" title="配货单位" />

                    <eui-text :value="getDictName('拆零标志', item.c_unpack)" title="拆零标志"
                        :oldValue="item.c_unpack_old_name" type="contrast" />
                    <eui-text :value="getDictName('价格敏感度', item.c_price_sens)" title="价格敏感度"
                        :oldValue="item.c_price_sens_old_name" type="contrast" />
                    <eui-text :value="getDictName('商品等级', item.c_grade)" title="商品等级"
                        :oldValue="item.c_grade_old_name" type="contrast" />
                    <eui-text :value="getDictName('价签类型', item.c_label_type)" title="价签类型"
                        :oldValue="item.c_label_type_old_name" type="contrast" />
                    <eui-text :value="item.c_od_day + getDictName('保质期单位', item.c_od_unit)"
                        :oldValue="item.c_od_day_unit_old_name" type="contrast" title="保质期" />
                </view>
            </eui-paging>
        </view>
    </view>
</template>

<script setup type="ts">
import { getRelatedName } from '../RdsViews/components/RelatedName.js';
import { getRdsUrl, rdsRequest } from '@/hooks/rdsServices';
import service from '@/common/service';
import { onMounted, ref } from "vue";

const rdsUrl = getRdsUrl();
const props = defineProps({
    data: {
        type: Object
    },
    windowWidth: {
        type: Number,
        default: 0,
    }
});
const rdsDict = ref([]);
const rdsUserDict = ref([]);
const mainData = ref({});
const detailData = ref([]);
const listHeight = ref(300);
const paging = ref(null);
const listManager = ref([]);
onMounted(async () => {
    await getListHeight();
    service.enjoyShowLoading('正在加载...');
    await getAllDicData();
    if (props.data.BillId) {
        await billQuery(props.data.BillId);
    }
    service.enjoyHideLoading();
})

// 获取列表高度
const getListHeight = async () => {
    uni.getSystemInfo({
        success: function (res) { // res - 各种参数
            let info = uni.createSelectorQuery().select(".footer-view");
            info.boundingClientRect(function (data) { //data - 各种参数
                //获取列表高度
                let height = res.windowHeight - (data?.top  || 0);
                //转换rpx
                listHeight.value = height / (res.windowWidth / 750) - 100;
            }).exec()
        }
    });
};
// 获取字典数据
const getAllDicData = async () => {
    rdsDict.value = await service.getLocalStorage('rdsDict');
    rdsUserDict.value = await service.getLocalStorage('rdsUserDict');
}
const getDictName = (key, value) => {
    const dictMap = rdsDict.value.find(x => x.c_class_name === key && x.c_id === value);
    return dictMap ? dictMap.c_name : ''
}
const getUserDictName = (key, value) => {
    const dictMap = rdsUserDict.value.find(x => x.c_class_name === key && x.c_id === value);
    return dictMap ? dictMap.c_name : ''
}
// 获取预约单据详情信息
const billQuery = async (billId) => {
    if (!billId) {
        paging.value.setLocalPaging([]);
        return;
    }
    const result = await rdsRequest(rdsUrl.bcApi, '商品信息变更单', 'EnjoyBillQuery', { BillId: billId }, '051');
    if (result.ds.length > 0) {
        mainData.value = result.ds[0];
        detailData.value = result.ds1;
        const oldDetailData = result.ds2;
        detailData.value.forEach(x => {
            let oldData = oldDetailData.find(o => o.c_gsort === x.c_gsort);
            x.c_name = `[${x.c_gcode}]${x.c_name || ''}`;
            x.c_name_old = `[${oldData.c_gcode}]${oldData.c_name || ''}`;
            x.c_gdsclass_type_old_name = getDictName('品类类型', oldData.c_gdsclass_type);
            x.c_pluno_old = oldData.c_pluno;
            x.c_plu_type_old = getDictName('称重/计件类型', oldData.c_plu_type);
            x.c_model_old = oldData.c_model;
            x.c_basic_unit_old = oldData.c_basic_unit;
            x.c_order_content_old = oldData.c_order_content;
            x.c_order_unit_old = oldData.c_order_unit;
            x.c_distr_content_old = oldData.c_distr_content;
            x.c_distr_unit_old = oldData.c_distr_unit;
            x.c_name_en_old = oldData.c_name_en;
            x.c_bcode = `${x.c_bcode ? `[${x.c_bcode}]${getRelatedName('rdsBrands', x.c_bcode) || ''}` : ''}`;
            x.c_bcode_old_name = `${oldData.c_bcode ? `[${oldData.c_bcode}]${getRelatedName('rdsBrands', oldData.c_bcode) || ''}` : ''}`;
            x.c_pycode_old = oldData.c_pycode;
            x.c_unpack_old_name = getDictName('拆零标志', oldData.c_unpack);
            x.c_price_sens_old_name = getDictName('价格敏感度', oldData.c_price_sens);
            x.c_grade_old_name = getUserDictName('商品等级', oldData.c_grade);
            x.c_label_type_old_name = getDictName('价签类型', oldData.c_label_type);
            x.c_od_day_unit_old_name = oldData.c_od_day + getDictName('保质期单位', oldData.c_od_unit);
        })
        paging.value.setLocalPaging(result.ds1);
    } else {
        paging.value.setLocalPaging([]);
    }
}
</script>

<style lang="scss" scoped>
@import '@/static/font/enjoyicon/iconfont.css';
@import '@/main.scss';

.footer-view {
    display: flex;
    flex: 1;
    flex-direction: column;
}

.paging-view {
    display: flex;
    flex: 1;
    flex-direction: column;
    background: $uni-bg-color-inverse;
    border-radius: 20rpx;
    margin: 10rpx 20rpx;
    padding: 20rpx;
}

.m-main-top {
    width: 100%;
    display: grid;
    grid-template-columns: 51% 49%;
}

.m-list-top {
    width: 100%;
    display: grid;
    grid-template-columns: 51% 49%;
    border-bottom: 1px solid #c8c7cc;
}

.image-min {
    width: 100%;
    height: 150rpx;
    opacity: 0.6;
    margin-top: -20rpx;
}

.no-data-text {
    text-align: center;
    color: #cccccc;
    font-size: 26rpx;
    margin-top: -20rpx;
}
</style>
